<template>
	<view class="page">
		<cus-navbar bgColor="#f8f8f8">
			<block slot="right">
				<view class="search">
					<input v-model="searchKey" type="text" class="input" placeholder="请输入书名|作者" @input="whenInput"
						@blur="addTag" />
					<text class="text" @tap="getBookByKey">搜索</text>
				</view>
			</block>
		</cus-navbar>
		<view v-if="!isSearched" class="searchKey">
			<view class="tag" v-for="(item,index) in keys" :key="index" @tap.stop.prevent="clickTag(item)">
				<text class="text">{{item}}</text>
				<image class="img" src="../../static/index/close.png" mode="scaleToFill"
					@tap.stop.prevent="deleteTag(index)">
				</image>
			</view>
		</view>
		<view v-else>
			<com-novel-list :list="items"></com-novel-list>
			<view v-if="items.length===0" class="noList">暂无结果</view>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapMutations
	}
	from 'vuex'
	export default {
		data() {
			return {
				searchKey: '',
				items: [],
				isSearched: false
			}
		},
		computed: {
			...mapGetters({
				'curSource': 'get_curSource',
				'keys': 'get_searchKey'
			}),
		},
		watch: {
			searchKey(nVal, oVal) {
				if (!nVal) {
					this.isSearched = false
					this.items = []
				}
			}
		},
		methods: {
			...mapMutations(['set_searchKey']),
			async getBookByKey() {
				if (!this.searchKey) {
					return
				}
				this.isSearched = true
				uni.showLoading({
					title: '加载中'
				})
				let res = await this.$store.dispatch('getBookByKey', this.searchKey)
				uni.hideLoading()
				if (res) {
					this.items = res
				}
			},
			novelTap(item) {
				uni.navigateTo({
					url: '/pages/public/bookDetail?id=' + item._id
				})
			},
			addTag() {
				if (!this.searchKey) {
					return
				}
				let index = this.keys.findIndex(item => item === this.searchKey)
				if (index === -1) {
					let tKeys = JSON.parse(JSON.stringify(this.keys))
					tKeys.push(this.searchKey)
					this.set_searchKey(tKeys)
				}
			},
			deleteTag(index) {
				let tKeys = JSON.parse(JSON.stringify(this.keys))
				tKeys.splice(index, 1)
				this.set_searchKey(tKeys)
			},
			clickTag(value) {
				this.isSearched = true
				this.searchKey = value
				this.getBookByKey()
			},
			whenInput() {
				const self = this
				this.$throttling(() => {
					self.getBookByKey()
				})
			}
		}
	}
</script>
<style lang="scss">
	.searchKey {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		flex-wrap: wrap;

		.tag {
			font-size: 24rpx;
			margin-top: 20rpx;
			margin-left: 20rpx;
			padding: 8rpx 16rpx;
			background-color: #f3f3f3;
			border-radius: 8rpx;

			display: flex;
			justify-content: flex-start;
			align-items: center;

			.img {
				width: 24rpx;
				height: 24rpx;
				margin-left: 20rpx;
			}
		}
	}

	.search {
		width: 100%;
		display: flex;
		justify-content: flex-start;
		align-items: center;

		.input {
			position: relative;
			width: 100%;
			background-color: #FFFFFF;
			padding: 8rpx 20rpx 8rpx 68rpx;
			border-radius: 40rpx;

			&::before {
				position: absolute;
				top: 50%;
				left: 4rpx;
				width: 60rpx;
				content: "\ea0e";
				display: block;
				font-size: 40rpx;
				font-family: uni;
				text-align: center;
				color: #cccccc;
				transform: translateY(-50%);
			}
		}

		.text {
			padding: 8rpx 20rpx;
			white-space: nowrap;
		}
	}

	.uni-input-placeholder {
		font-size: 30rpx;
		color: #cccccc;
	}

	.noList {
		text-align: center;
		margin-top: 200rpx;
	}
</style>
